/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ICMPPanel.java
 *
 * Created on 2010-01-23, 23:00:27
 */

package efe.gui;

import efe.bean.EfeByteValue;
import efe.bean.EfeNumberValue;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Vector;
import jpcap.packet.ICMPPacket;

/**
 * Panel z nagłówkiem protokołu IMCP.
 * @author root
 */
public class ICMPPanel extends javax.swing.JPanel implements Serializable {

    /** Creates new form ICMPPanel */
    public ICMPPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        typeValue = new efe.bean.EfeByteValue();
        codeValue = new efe.bean.EfeByteValue();
        idValue = new efe.bean.EfeNumberValue();
        seqValue = new efe.bean.EfeNumberValue();
        jPanel1 = new javax.swing.JPanel();
        efeEditorField1 = new efe.bean.EfeEditorField();
        efeEditorField2 = new efe.bean.EfeEditorField();
        efeEditorBlankField1 = new efe.bean.EfeEditorBlankField();
        efeEditorField3 = new efe.bean.EfeEditorField();
        efeEditorField4 = new efe.bean.EfeEditorField();

        typeValue.setLength(1);

        codeValue.setLength(1);

        idValue.setLimit(65535);

        seqValue.setLimit(65535);

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setLayout(new java.awt.GridLayout(2, 2));

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        efeEditorField1.setText("Type = 0x00 ");
        efeEditorField1.setLabelText("Type");
        efeEditorField1.setValue(typeValue);
        jPanel1.add(efeEditorField1);

        efeEditorField2.setText("Code = 0x00 ");
        efeEditorField2.setLabelText("Code");
        efeEditorField2.setValue(codeValue);
        jPanel1.add(efeEditorField2);

        add(jPanel1);

        efeEditorBlankField1.setText("Checksum");
        add(efeEditorBlankField1);

        efeEditorField3.setLabelText("ID");
        efeEditorField3.setValue(idValue);
        efeEditorField3.setVerifyInputWhenFocusTarget(false);
        add(efeEditorField3);

        efeEditorField4.setLabelText("Sequence");
        efeEditorField4.setValue(seqValue);
        add(efeEditorField4);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Tworzy pakiet IMCP z pól nagłówka, gotowy do tego żeby osadzić go
     * w warstwie sieciowej.
     * @return pakiet
     */
    public ICMPPacket getICMPPacket() {
        ICMPPacket p = new ICMPPacket();
        p.type = ((byte[]) typeValue.getValue())[0];
        p.code = ((byte[]) codeValue.getValue())[0];
        p.id = (short) idValue.getValue();
        p.seq = (short) seqValue.getValue();
        return p;
    }

    /**
     * Zwraca wektor serializowalnych wartości, które należy zapisać do pliku.
     * @return kolekcja wartości nagłówka
     */
    public Vector<Object> getValuesVector() {
        Vector<Object> v = new Vector<Object>();
        v.add(codeValue);
        v.add(idValue);
        v.add(seqValue);
        v.add(typeValue);
        return v;
    }

    /**
     * Zapisuje dane z wektora do pól nagłówka.
     * @param v wektor wartości z elementami nagłówka w kolejności takiej samej
     * jak ta zwrócona przez metodę getValuesVector()
     */
    public void setValuesFromVector(Vector<Object> v) {
        Iterator<Object> i = v.iterator();
        codeValue = (EfeByteValue) i.next();
        efeEditorField2.setValue(codeValue);
        idValue = (EfeNumberValue) i.next();
        efeEditorField3.setValue(idValue);
        seqValue = (EfeNumberValue) i.next();
        efeEditorField4.setValue(seqValue);
        typeValue = (EfeByteValue) i.next();
        efeEditorField1.setValue(typeValue);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private efe.bean.EfeByteValue codeValue;
    private efe.bean.EfeEditorBlankField efeEditorBlankField1;
    private efe.bean.EfeEditorField efeEditorField1;
    private efe.bean.EfeEditorField efeEditorField2;
    private efe.bean.EfeEditorField efeEditorField3;
    private efe.bean.EfeEditorField efeEditorField4;
    private efe.bean.EfeNumberValue idValue;
    private javax.swing.JPanel jPanel1;
    private efe.bean.EfeNumberValue seqValue;
    private efe.bean.EfeByteValue typeValue;
    // End of variables declaration//GEN-END:variables

}
